



import 'package:zfth/pages/Tabbar.dart';
import 'package:zfth/pages/user/User_login.dart';
import 'package:flutter/material.dart';
import '../api/UserApi.dart';
import '../utils/SPutil.dart';
import '../utils/Settings.dart';
import '../utils/StoreData.dart';
import '../utils/Utils.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
class IndexPage extends StatefulWidget{
  const IndexPage({super.key});

  @override
  State<IndexPage> createState() =>_IndexPageState();

}

class _IndexPageState extends State<IndexPage> with AutomaticKeepAliveClientMixin{

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;

  late Future getInfo;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  ///获取用户信息
  Future queryInfo()async{
    return await UserApi.queryUserInfo();
  }

  Widget initJump(BuildContext context, AsyncSnapshot snapshot) {
    if(snapshot.connectionState == ConnectionState.waiting){
      return SizedBox(
        width: double.infinity, // 屏幕宽度
        height: double.infinity, // 屏幕高度
        child: Image.asset("images/bg.png",fit: BoxFit.cover,),
      );
    }else{
      dynamic res  = snapshot.data;
      if(Utils.isEmpty(res)){
        EasyLoading.showError("服务器连接失败");
        return const UserLoginPage();
      }else if(!res["success"]){
        EasyLoading.showError(res["message"]);
        return const UserLoginPage();
      }

      StoreData.menuList=res["result"]["menus"];
      StoreData.sysEmp = res["result"]["sysEmp"];
      StoreData.token = res["result"]["token"];
      StoreData.buttons =res["result"]["buttons"];
      SPutil.setLocalStorage(Settings.localStorageKeys["token"], res["result"]["token"]);
      SPutil.setLocalStorage(Settings.localStorageKeys["empNo"], res["result"]["sysEmp"]["empNo"]);
      return const TabbarPage();
    }

  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    // print(SPutil.getLocalStorage(Settings.localStorageKeys["token"]));
    //判断是否有TOKEN
    if(Utils.isEmpty(SPutil.getLocalStorage(Settings.localStorageKeys["token"]))){
      return const UserLoginPage();
    }else{
      getInfo = queryInfo();
      return FutureBuilder(
        builder: initJump,
        future: getInfo,
      );
    }

  }
}